package com.xiyuan.smartutils.streams;

import java.io.IOException;
import java.io.InputStream;

/**
 * 记录输入流长度的自定义输入流
 *
 * @version v1.0.0 @author lgz 2017-6-14 新建与整理
 */
public class LengthInputStream extends InputStream {
    private InputStream in;
    private long length;
    
    public LengthInputStream(InputStream in) {
        this.in = in;
    }
    
    public long length() {
        return length;
    }
    
    public int read() throws IOException {
        int c = in.read();
        if (c != -1) {length++;}
        return c;
    }
    
    public int read(byte[] b, int off, int len) throws IOException {
        int result = in.read(b, off, len);
        if (result != -1) {length += len;}
        
        return result;
    }
}
